package online.inote.modules.user.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExcessiveAttemptsException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import online.inote.common.core.BaseController;
import online.inote.common.core.Page;
import online.inote.modules.user.entity.UserEntity;
import tk.mybatis.mapper.entity.Example;

/**
 * <p>
 * Desc: 用户Controller
 * </p>
 *
 * @author XQF SUI
 * @created 2018年8月25日 下午2:09:47
 * @version 1.0
 */
@Controller
@RequestMapping(value = {"/", "user"})
public class UserController extends BaseController<UserEntity> {

	/**
	  * <p>
	  * Desc: 跳转到登录页面
	  * </p>
	  *
	  * @author XQF SUI
	  * @created 2018年8月25日 下午2:11:49
	  * @return
	 */
	@RequestMapping(value = {"/", "login"})
	public String login() {
		logger.info("跳转到登陆页面");
		return this.view("login");
	}
	
	/**
	  * <p>
	  * Desc: 登录认证
	  * </p>
	  *
	  * @author XQF SUI
	  * @created 2018年8月25日 下午2:12:20
	  * @return
	 */
	@RequestMapping(value = "signIn", method = RequestMethod.POST)
	public String signIn(UserEntity user) {
		String resultMsg = "验证通过";
		
		UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());  
		token.setRememberMe(user.isRememberMe());
        
        Subject subject = SecurityUtils.getSubject();
        
        try {  
            logger.info("开始对用户[ {} ]进行登录验证...", user.getUsername());  
            subject.login(token);  
        }catch(UnknownAccountException e){  
            resultMsg = "验证未通过：未知账户";
        }catch(IncorrectCredentialsException e){  
            resultMsg = "验证未通过：错误的凭证";
        }catch(LockedAccountException e){  
            resultMsg = "验证未通过：账户已锁定";
        }catch(ExcessiveAttemptsException e){  
            resultMsg = "验证未通过：错误次数过多";
        }catch(AuthenticationException e){  
            resultMsg = "验证未通过：用户名或密码不正确";
            logger.error("登录异常:", e);
        }  
        
        logger.info("用户[ {} ]登录验证...{}", user.getUsername(), resultMsg);
        
        //验证是否登录成功  
        if(subject.isAuthenticated()){  
             return view("index");
        } else {
        	token.clear();
        	return "error/404";
        }
	}
	
	/**
	 * <p>
	 * Desc: 退出登录
	 * </p>
	 *
	 * @author XQF SUI
	 * @created 2017年12月4日 下午1:29:34
	 * @return
	 */
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout() {
        SecurityUtils.getSubject().logout();
        return view("login");
	}
	
	@ResponseBody
	@RequestMapping(value = "getPage", method = RequestMethod.GET)
	public Page<UserEntity> getPage(Page<UserEntity> page) {
		service.selectPageByExampleNotCount(new Example(UserEntity.class), page);
		return page;
	}
}